﻿using System;
using System.Linq;
using Core.Contract.DataContract;
using www.Models;

namespace Data.DAO
{
    public class MemberDAO//:BaseDAO<RELAXEntities,RelaxMember>
    {
        #region value
        private readonly RelaxDataContext _context = new RelaxDataContext();
        #endregion
    
        #region Ham lay du Lieu
        public IQueryable<RelaxMemberDataContract> GetMbDataContracts()
        {
            var query = from c in _context.RelaxMembers
                        select new RelaxMemberDataContract
                        {
                           Id=Convert.ToInt32(c.id),
                            Gender = Convert.ToInt32(c.gender),
                            Birthday = Convert.ToDateTime(c.birthday),
                            Hometown = c.hometown,
                            Degree = c.degree,
                            Email = c.mail,
                            Tel = c.tel,
                            Image = c.image,
                            Active = Convert.ToInt32(c.active)
                        };
            return query;
        }
        public IQueryable<RelaxLoginsDataContract> GetMemberDataContracts()
        {
            var query = from c in _context.RelaxMembers
                        join u in _context.RelaxLogins on c.id equals u.memberid
                select new RelaxLoginsDataContract
                {
                    Id = Convert.ToInt32(u.id),
                    MemberId = Convert.ToInt32(c.id),
                    MemberName = c.name,
                    UserName = u.username,
                    Password = u.password,
                    Gender = Convert.ToInt32(c.gender),
                    Birthday = Convert.ToDateTime(c.birthday),
                    Hometown = c.hometown,
                    Degree = c.degree,
                    Email = c.mail,
                    Tel = c.tel,
                    Image = c.image,
                    Active = Convert.ToInt32(c.active)
                };
            return query;
        }
        public IQueryable<RelaxLoginsDataContract> GetMemberDataContracts(string sUsername)
        {
            var query = from c in _context.RelaxMembers
                        join u in _context.RelaxLogins.Where(l => l.username == sUsername) on c.id equals u.memberid
                        select new RelaxLoginsDataContract
                        {
                            Id = Convert.ToInt32(u.id),
                            MemberId = Convert.ToInt32(c.id),
                            MemberName = c.name,
                            UserName = u.username,
                            Password = u.password,
                            Gender = Convert.ToInt32(c.gender),
                            Birthday = Convert.ToDateTime(c.birthday),
                            Hometown = c.hometown,
                            Degree = c.degree,
                            Email = c.mail,
                            Tel = c.tel,
                            Image = c.image,
                            Active = Convert.ToInt32(c.active)
                        };
            return query;
        }
        #endregion 
     
        #region Add
        public void Add(RelaxMember member)
        {
            _context.RelaxMembers.InsertOnSubmit(member);
            _context.SubmitChanges();

        }
        #endregion
    
        #region Ham Update

        public void Update(RelaxMember member)
        {
            var query = _context.RelaxMembers.Where(c => c.id == member.id);
            if (query.Any())
            {
                var firstOrdefault = query.FirstOrDefault();
                if (firstOrdefault != null)
                {
                   
                    firstOrdefault.name = member.name;
                    firstOrdefault.gender = member.gender;
                    firstOrdefault.hometown = member.hometown;
                    firstOrdefault.birthday = member.birthday;
                    firstOrdefault.degree = member.degree;
                    firstOrdefault.mail = member.mail;
                    firstOrdefault.tel = member.tel;
                    firstOrdefault.image = member.image;
                    firstOrdefault.active = member.active;
                }
                _context.SubmitChanges();
            }
        }
        #endregion 

        #region Ham Delete

        public void Delete(long sId)
        {
            var query = _context.RelaxMembers.Where(c => c.id == sId);
            if (query.Any())
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                _context.RelaxMembers.DeleteOnSubmit(query.FirstOrDefault());
                _context.SubmitChanges();
            }
        }
        #endregion
    }
}
